//
//  CitySelectViewController.m
//  Zyachting
//
//  Created by NGUYEN THE HUNG on 10/29/12.
//
//

#import "CitySelectViewController.h"

@interface CitySelectViewController ()

@end

@implementation CitySelectViewController
@synthesize tableView,items,isNorth,currentSelectedIndex;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString* filePath = [[NSBundle mainBundle] pathForResource:@"contents" ofType:@"plist"];
 NSArray* cities = [[NSMutableDictionary dictionaryWithContentsOfFile:filePath] objectForKey:@"cities"];
    
    if (self.isNorth) {
        self.items = [cities subarrayWithRange:NSMakeRange(1, 3)];
        self.navigationItem.title = @"Costa Blanca North";
    }else{
        self.items = [cities subarrayWithRange:NSMakeRange(4, cities.count - 4)];
        self.navigationItem.title = @"Costa Blanca South";        
    }
    UIBarButtonItem* back = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:back];
    [back release];
    self.navigationItem.backBarButtonItem.title = @"Back";
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark TableView DataSource Method
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    [cell setAccessoryType:UITableViewCellAccessoryNone];
    if (self.currentSelectedIndex == indexPath.row) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
    cell.textLabel.text = [self.items objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    return cell;

}

#pragma mark -
#pragma mark TableView Delegate Method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.currentSelectedIndex = indexPath.row;
    [self.tableView reloadData];
}


@end
